package edu.cicese.alz.swing.dialogs;

import edu.cicese.alz.swing.SwingUtil;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 29/07/12
 * Time: 09:23 PM
 */
public class ErrorDialog extends CustomDialog {
	private String text;

	public ErrorDialog(Component owner, String title, String text) {
		super((JFrame) SwingUtilities.getRoot(owner), title, true);

		this.text = text;

		setMin(350, 125);
		setInitialLocation();
		setResizable(false);

		initComponents();
		setVisible(true);
	}

	private void initComponents() {
		JButton btnOK = SwingUtil.createButtonBackground("OK", 71, 22, null,
				getIcon("bg"), getIcon("bgO"), getIcon("bgP"));
		btnOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionOK();
			}
		});

		JLabel lblIcon = new JLabel(getIcon("error"));
		JLabel lblText = SwingUtil.createLabel(SwingUtil.convertToMultiline(text), SwingUtil.fontMain);

		Box boxButtons = Box.createHorizontalBox();
		boxButtons.add(Box.createHorizontalGlue());
		boxButtons.add(btnOK);
		boxButtons.add(Box.createHorizontalGlue());

		JPanel pContent = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		// Row 1
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 10);
		pContent.add(lblIcon, gbc);

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 0, 0, 0);
		pContent.add(lblText, gbc);

		// Row 2
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 0, 0, 0);
		pContent.add(boxButtons, gbc);

		pContent.setBackground(Color.WHITE);
		pContent.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		setContentPane(pContent);
	}

	public static void main(String[] a) {
		new ErrorDialog(new JFrame(), "Error", "Error dialog").setVisible(true);
	}
}
